#include "RegenTile.h"
#include "ImgMgr.h"
#include "Player.h"

RegenTile::RegenTile() : Tile()
{

}

void RegenTile::init(const sf::Vector2f position, ImgMgr *imageManager, Level *level)
{
    m_skin = imageManager->getSprite(4);
    m_skin.setPosition(position);
    m_skin.setScale(2, 2);
    m_name = "Regen";
    m_bufferRegain.loadFromFile("ressource/regain.wav");
    m_soundRegain.setBuffer(m_bufferRegain);
}

bool RegenTile::block() const
{
    return false;
}

void RegenTile::tick(Player *player, Level *level)
{
    if(player->getPosition().y < getPosition().y + 6)
    {
        int xPlayer = player->getPosition().x;
        int yPlayer = player->getPosition().y;
        int distance = std::sqrt((xPlayer - getPosition().x) * (xPlayer - getPosition().x) + (yPlayer - getPosition().y)*(yPlayer - getPosition().y));
        if(distance < 50)
        {
            if(m_clockRegen.getElapsedTime().asMilliseconds() > 1000 && player->getEnergy() < 10)
            {
                player->modifyEnergy(0.5);
                m_soundRegain.play();
                m_clockRegen.restart();
            }
        }
    }
}
